package com.ruoyi.web.controller.property.garbage;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.GarbageRecordDetail;
import com.ruoyi.common.core.domain.entity.GarbageType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.GarbageRecordDetailService;
import com.ruoyi.system.service.GarbageTypeService;
import com.ruoyi.system.service.resp.GarbageMonthStatDTO;
import com.ruoyi.system.service.resp.GarbageMonthStatDetailDTO;
import com.ruoyi.system.service.resp.GarbageTodayStatDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: ruohao
 * @Date: 2023/9/9
 **/
@RestController
@RequestMapping("/property/statistic")
public class StatisticController extends BaseController {
    @Resource
    private GarbageRecordDetailService garbageRecordDetailService;
    @Resource
    private GarbageTypeService garbageTypeService;

    @GetMapping("/month")
    public AjaxResult list(String year, String location)
    {
        GarbageType garbageType = new GarbageType();
        garbageType.setLocation(location);
        List<GarbageType> garbageTypes = garbageTypeService.selectGarbageTypeList(garbageType);
        List<List<Integer>> result = new ArrayList<>();

        for (GarbageType type : garbageTypes) {
            List<Integer> one = new ArrayList<>();
            for (int i = 1; i < 13; i++) {
                Calendar calendar = Calendar.getInstance();
                if (StringUtils.isEmpty(year)) {
                    year = calendar.get(Calendar.YEAR) + "";
                }
                calendar.set(Integer.parseInt(year), i, 1);
                int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                Integer integer = garbageRecordDetailService.selectSumByLocationAndTime(year + "-" + i + "-01 00:00:00", year + "-" + i + "-" + lastDay + " 23:59:59",
                        type.getId(), location);
                one.add(integer == null ? 0 : integer);
            }
            result.add(one);
        }
        GarbageMonthStatDetailDTO garbageMonthStatDetailDTO = new GarbageMonthStatDetailDTO();
        garbageMonthStatDetailDTO.setTimeSeries(getTimeSeries());
        garbageMonthStatDetailDTO.setKeySeries(getKeySeries(garbageTypes));
        garbageMonthStatDetailDTO.setData(result);
       return AjaxResult.success(garbageMonthStatDetailDTO);
    }

    private List<String> getTimeSeries() {
        List<String> result = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            result.add(i + "月");
        }
        return result;
    }

    private List<String> getKeySeries(List<GarbageType> garbageTypes) {
        List<String> result = new ArrayList<>();
        for (GarbageType garbageType : garbageTypes) {
            result.add(garbageType.getType());
        }
        return result;
    }

    @GetMapping("/today")
    public TableDataInfo listToday(String location)
    {
        GarbageRecordDetail query = GarbageRecordDetail.builder()
                .location(location)
                .build();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        query.setCreateTime(calendar.getTime());
        List<GarbageRecordDetail> garbageRecordDetails = garbageRecordDetailService.selectToday(query);
        return getDataTable(fillToDTO(garbageRecordDetails));
    }

    private List<GarbageTodayStatDTO> fillToDTO(List<GarbageRecordDetail> garbageRecordDetails) {
        if (CollectionUtils.isEmpty(garbageRecordDetails)) {
            return Collections.emptyList();
        }
        List<Long> typeIds = garbageRecordDetails.stream().map(GarbageRecordDetail::getGarbageTypeId).collect(Collectors.toList());
        Map<Long, GarbageType> garbageTypeMap = garbageTypeService.selectByIds(typeIds).stream().collect(Collectors.toMap(GarbageType::getId, g -> g));
        return garbageRecordDetails.stream().map(g -> {
            GarbageTodayStatDTO garbageTodayStatDTO = new GarbageTodayStatDTO();
            garbageTodayStatDTO.setId(g.getId());
            garbageTodayStatDTO.setUnit(g.getUnit());
            garbageTodayStatDTO.setAmount(g.getAmount());
            garbageTodayStatDTO.setLocation(g.getLocation());
            garbageTodayStatDTO.setGarbageTypeName(Optional.ofNullable(garbageTypeMap.get(g.getGarbageTypeId())).map(GarbageType::getType).orElse(null));
            garbageTodayStatDTO.setCreateTime(g.getCreateTime());
            return garbageTodayStatDTO;
        }).collect(Collectors.toList());
    }


    public static void main(String[] args) {
        RestTemplate restTemplate=new RestTemplate();
        Map params=new HashMap<>();
        params.put("key","SqvPQfqvOuoqPAavL");
        params.put("location", "宁夏银川");
        ResponseEntity forEntity = restTemplate.getForEntity("https://api.seniverse.com/v3/weather/now.json?key={key}&location={location}", String.class, params);
        JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody().toString());
        System.out.println(jsonObject);
    }
    @GetMapping("/weather")
    public AjaxResult weather()
    {
        RestTemplate restTemplate=new RestTemplate();
        Map params=new HashMap<>();
        params.put("key","SqvPQfqvOuoqPAavL");
        params.put("location", "宁夏银川");
        ResponseEntity forEntity = restTemplate.getForEntity("https://api.seniverse.com/v3/weather/now.json?key={key}&location={location}", String.class, params);
        JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody().toString());
        return AjaxResult.success(jsonObject);
    }

}
